package net.misp.beta.nomilog.model;

import java.util.ArrayList;

import com.google.android.maps.GeoPoint;

import net.misp.beta.nomilog.R;
import net.misp.beta.nomilog.util.StorePrefix;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class StoreAdapter extends BaseAdapter {

	private static StoreAdapter mAdapter=null;
	Context mContext;
	ArrayList<StoreData> mDataList;
	DBStoreAdapter dbAdapter;
	SQLiteDatabase mDB;
	StoreHelper helper;

	public class ViewHolder{
		ImageView typeIcon;
		TextView storeName;
		TextView storeAddr;

	}

	private StoreAdapter(Context context){
		mContext=context;
		helper=new StoreHelper(mContext);
		mDB=helper.getReadableDatabase();
		mDataList=DBStoreAdapter.getList(mDB, "id");
	}

	public void setData(ArrayList<StoreData> newDataList){
		mDataList.clear();
		mDataList.addAll(newDataList);
	}

	@Override
	public int getCount() {
		return mDataList.size();
	}

	@Override
	public StoreData getItem(int arg0) {
		return mDataList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		ViewHolder holder;
		StoreData store = getItem(position);

		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			convertView = inflater.inflate(R.layout.row_item, null);

			holder = new ViewHolder();

			holder.typeIcon = (ImageView) convertView
					.findViewById(R.id.imv_icon);
			holder.storeName = (TextView) convertView
					.findViewById(R.id.lbl_store_name);
			holder.storeAddr = (TextView) convertView
					.findViewById(R.id.lbl_store_addr);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

//		switch (store.getGenre()) {
//		case StorePrefix.TYPE_CAFE:
//			holder.typeIcon.setImageResource(StorePrefix.ICON_CAFE);
//			break;
//		case StorePrefix.TYPE_RESUTAURANT:
//			holder.typeIcon.setImageResource(StorePrefix.ICON_RESUTAURANT);
//			break;
//		case StorePrefix.TYPE_BAR:
//			holder.typeIcon.setImageResource(StorePrefix.ICON_BAR);
//			break;
//		case StorePrefix.TYPE_FASTFOOD:
//			holder.typeIcon.setImageResource(StorePrefix.ICON_FASTFOOD);
//			break;
//		default:
			holder.typeIcon.setImageResource(R.drawable.ic_launcher);
//			break;
//		}
		holder.storeName.setText(store.getName());
		holder.storeAddr.setText(store.getAddress());

		return convertView;
	}

	public static StoreAdapter getInstance(Context context) {
		if(mAdapter==null){
			mAdapter=new StoreAdapter(context);
		}
		return mAdapter;
	}

	public void addItem(StoreData data) {
		DBStoreAdapter.addItem(mDB,data);
		mDataList=DBStoreAdapter.getList(mDB, StorePrefix.FLAG_NAME);
	}

	public void updateItem(StoreData data) {
		DBStoreAdapter.updateItem(mDB,data);
		mDataList=DBStoreAdapter.getList(mDB, StorePrefix.FLAG_NAME);
	}

	public void deleteItem(int id) {
		DBStoreAdapter.deleteItem(mDB,id);
		mDataList=DBStoreAdapter.getList(mDB, StorePrefix.FLAG_NAME);
	}

	public ArrayList<GeoPoint> getPinList() {
		ArrayList<GeoPoint> geoList=new ArrayList<GeoPoint>();
		GeoPoint point;
		
		for(StoreData data:mDataList){
			point=new GeoPoint(data.getLatitude(), data.getLongitude());
			geoList.add(point);
		}
		
		return geoList;
	}

}
